//
//  AdViewController.m
//  ZYYD-Iphone
//
//  Created by yuzhou on 12-9-17.
//  Copyright (c) 2012年 yuzhou. All rights reserved.
//

#import "AdViewController.h"
#import "DataRequest.h"
#import "SingleAdPageView.h"

@interface AdViewController ()

@end

@implementation AdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)showAds:(NSArray *)ads
{
    self.adArray = ads;
    [self stopAutoScroll];
    
    for (UIView *subView in adScrollView.subviews) {
        [subView removeFromSuperview];
    }
    
    adScrollView.contentSize = CGSizeMake(320*ads.count, 148);
    adScrollView.contentOffset = CGPointMake(0, 0);
    for (NSDictionary *infoDic in ads) {
        SingleAdPageView *singleAdPageView = [[[SingleAdPageView alloc] initWithFrame:CGRectMake(320*[ads indexOfObject:infoDic], 0, 320, 150)] autorelease];
        singleAdPageView.infoDic = infoDic;
        [adScrollView addSubview:singleAdPageView];
        NSLog(@"广告》》》%@》》》%@",[infoDic valueForKey:@"title"],[infoDic valueForKey:@"description"]);
    }
    adPageControl.numberOfPages = ads.count;
    adPageControl.currentPage = 0;
    
    [self startAutoScroll];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = adScrollView.contentOffset.x/320;
//    NSLog(@"page:%d",page);
    adPageControl.currentPage = page;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollToPage:(int)page
{
    [adScrollView setContentOffset:CGPointMake(320*page, 0) animated:YES];
}

- (void)scrollToNextPage:(NSTimer *)timer
{
    if (adPageControl.numberOfPages > 1 && !adScrollView.tracking) {
        int nextPage = adPageControl.currentPage < adPageControl.numberOfPages-1 ? adPageControl.currentPage+1 : 0;
        [self scrollToPage:nextPage];
    }
}

- (void)startAutoScroll
{
    if (scrollTimer == nil) {
        scrollTimer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:scrollTimer forMode:NSDefaultRunLoopMode];
//        [scrollTimer fire];
    }
}

- (void)stopAutoScroll
{
    [scrollTimer invalidate];
    scrollTimer = nil;
}

- (void)viewDidUnload
{
    [adScrollView release];
    adScrollView = nil;
    [adPageControl release];
    adPageControl = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [self stopAutoScroll];
    [adScrollView release];
    [adPageControl release];
    [_adArray release];
    [super dealloc];
}
@end
